﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace TMS
{
    public partial class EditWarehouseProductsControl : UserControl
    {

        Product prod = new Product();
        Int32 ProductID;
        Users ActiveUser = Program.GetActiveUser;

        public EditWarehouseProductsControl()
        {
            InitializeComponent();
        }

        public EditWarehouseProductsControl(DataGridViewRow row)
        {
            InitializeComponent();
            txtName.Text = row.Cells[1].Value.ToString();
            txtDescriptin.Text = row.Cells[2].Value.ToString();
            txtQuantity.Text = row.Cells[3].Value.ToString();
            txtBarcode.Text = row.Cells[4].Value.ToString();
            lblUnit.Text = row.Cells[6].Value.ToString();
            txtPrice.Text = row.Cells[5].Value.ToString();
            ProductID = Convert.ToInt32(row.Cells[7].Value);
            if (ActiveUser.GetName() == "Администратор")
            {
                txtQuantity.Enabled = true;
                lblWarehouses.Visible = true;
                cmbWarehouses.Visible = true;
            }
            cmbWarehouses.ValueMember = "name";
            FillWharehouses();

           
        }

        private void brnSave_Click(object sender, EventArgs e)
        {
            prod.UpdateWarehouseAvgPrice(ProductID, Convert.ToDecimal(txtPrice.Text));
            if (ActiveUser.GetName() == "Администратор")
                if (cmbWarehouses.SelectedValue.ToString() != "сите")
                    prod.UpdateWarehouseQuantity(ProductID, cmbWarehouses.SelectedValue.ToString(), Convert.ToInt32(txtQuantity.Text) - prod.GetProductQuantityFromWarehouse(ProductID, cmbWarehouses.SelectedValue.ToString()));
                else MessageBox.Show("Изберете магацин");
            Program.GetMainForm.ShowControl(ControlsEnum.WAREHOUSE_CONTROL);
            this.Dispose();
        }

        private void btnGenerateBarcode_Click(object sender, EventArgs e)
        {
           
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) this.Dispose();
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public void FillWharehouses()
        {
            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            DataSet table = new DataSet();
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();
            string sqlSelectAll = "SELECT * from warehouses";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);
            MyDA.Fill(table);
            mysqlCon.Close();
            cmbWarehouses.DataSource = table.Tables[0];
        }


        private void cmbWarehouses_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbWarehouses.SelectedValue.ToString()!="сите")
                    txtQuantity.Text = prod.GetProductQuantityFromWarehouse(ProductID, cmbWarehouses.SelectedValue.ToString()).ToString();
                else
                    txtQuantity.Text = prod.GetProductQuantityFromWarehouse(ProductID).ToString();
            }
            catch
            {
            }

        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }
    }
}
